* this script searches SReg for owners based on the results of JSCReg search

* first, add individual owners if they are present in JSCReg
* load the list of firms to search owners, merge with SReg
use for_SReg_owner_search_ind_owners.dta, clear
merge m:m okpo entity owner_country using SReg_working.dta
keep if _merge==3

* remove individual owners with missing id and name
drop if missing(investor_code) & missing(owner_name)

* remove duplicate entries
if _N > 0 {
sort okpo investor_code owner_name owner_country entity
quietly by okpo investor_code owner_name owner_country entity:  gen dup = cond(_N==1,0,_n) 
drop if dup>1
drop dup
}

* rename variables, drop irrelevant ones
rename okpo prev_id
drop fund_date correction_date _merge
rename investor_code id
rename owner_country country
rename owner_name name
gen owner = 1
order name entity country owner prev_id prev_name prev_entity prev_country Delo_UP_firm, after(id)
label var prev_id "id of previous step entity"
label var prev_name "name of entity from previous step"
label var prev_country "country of entity from previous step"
label var prev_entity "type of entity from previous step"
label var owner "owner of previous step entity"
save "owners_from_SReg.dta", replace

* search for all owners of firms not present in JSCReg
* load the list of firms to search owners, merge with SReg
use for_SReg_owner_search_all_owners.dta, clear
merge m:m okpo entity using SReg_working.dta
keep if _merge==3

* remove individual owners with missing id and name
drop if missing(investor_code) & missing(owner_name) & entity == 0

* remove duplicate entries
if _N > 0 {
sort okpo investor_code owner_name owner_country entity
quietly by okpo investor_code owner_name owner_country entity:  gen dup = cond(_N==1,0,_n) 
drop if dup>1
drop dup
}

* rename variables, drop irrelevant ones
rename okpo prev_id
label var prev_id "id of previous step entity"
drop fund_date correction_date _merge
save "owners_SReg_matched.dta", replace

* save owners with non-missing names
keep if !missing(owner_name) | entity == 0
if _N > 0 {
sort prev_id investor_code entity owner_country
quietly by prev_id investor_code entity owner_country:  gen dup = cond(_N==1,0,_n) 
drop if dup>1
drop dup
}
save "owners_SReg_known.dta", replace

* some owners do not have names in entries, but they have names in other entries
* identify these owners (Ukrainian firms)
merge m:m prev_id investor_code entity owner_country using "owners_SReg_matched.dta"
keep if _merge==2
drop _merge
drop if owner_country != 804
drop if entity == 0

* search for missing names among firms in the SReg
if _N > 0 {
rename investor_code okpo
merge m:m okpo using SReg_firm_names.dta
drop if _merge==2
drop _merge

* drop duplicate entries
sort prev_id okpo owner_country entity firm_name
quietly by prev_id okpo owner_country entity firm_name:  gen dup = cond(_N==1,0,_n) 
drop if dup > 1
drop dup

* replace missing names if found
rename okpo investor_code
replace owner_name = firm_name if owner_name == "." | missing(owner_name)
drop firm_name
}

* add owners with replaced names to owners with known names, clean up variables
append using "owners_SReg_known.dta"
rename investor_code id
rename owner_country country
rename owner_name name
gen owner = 1
order name entity country owner prev_id prev_name prev_entity prev_country Delo_UP_firm, after(id)
label var prev_name "name of entity from previous step"
label var prev_country "country of entity from previous step"
label var prev_entity "type of entity from previous step"
label var owner "owner of previous step entity"
append using "owners_from_SReg.dta"
save "owners_from_SReg.dta", replace

* remove temporary files
erase "owners_SReg_known.dta"
erase "owners_SReg_matched.dta"